
#' update the HTML 5.1 dir
#'
#' @param continue A logical value. Where continue an unfinished updating or 
#'     start a complete new updating.
#' @param force A logical value. Even continue is TRUE, update won't work if it 
#'     hasn't been a month since the last updating. You can use force = TRUE to 
#'     suppress the default behavior. 
#'
#' @return nothing
#' @export 
update <- function(continue = TRUE, force = FALSE) {
library(lubridate)
library(readr)
library(stringr)

get_old_date <- function() {
    old.date <- lubridate::ymd(stringr::str_split(getwd(), ' ')[[1]][3])
}

#copy old dir
new.dir <- paste('../HTML 5.1', stamp("(2016.02.03)")(Sys.Date()))
dir.create(new.dir)
file.copy(".", new.dir, recursive = T)
#download new files and overwrite 
file <- dir(pattern = "html$")
file = file[file != 'default.html']
source <- paste0('http://www.w3.org/TR/html51/', file)
dest <- file.path(new.dir, file)
fail <- character(length(file))
for (i in seq_along(file)) {
    tryCatch(download.file(source[i], dest[i]), 
             error = function(e) {fail[i] = file[i]}
    )
}
#backup and condense files
file.copy(new.dir, '~', recursive = T)
##extract all css and merge into an external file
html <- str_replace_all(read_file(dest[1]), fixed('\r\n'), '\n')
default.css <- str_extract_all(html, '<style[\\w\\W]*?>[\\w\\W]*?</style>')[[1]]
default.css <- str_replace_all(default.css, '</?style[\\w\\W]*?>', '')
writeLines(default.css, paste0(new.dir,'/styles/default.css'), useBytes = T)
for (i in seq_along(file)) {
    html = read_file(dest[i])
    html = str_replace_all(html, fixed('\r\n'), '\n')
    html = str_replace_all(html, 'https://www.w3.org/scripts/TR/2016', 'js')
    html = str_replace_all(html, 'http://www.w3.org/StyleSheets/TR/2016', 'styles')
    ##remove two redundant big chunks
    html = str_replace(html, '<aside class="dfn-panel"[\\w\\W]*</aside>', '')
    html = str_replace(html, '<script>/\\* script-dfn-panel \\*/[\\w\\W]*</script>', '')
    ##replace the first <style> with corresponding <link>
    html = str_replace(html, '<style[\\w\\W]*?>[\\w\\W]*?</style>', 
                       '<link rel="stylesheet" href=styles.default.css>')
    ##remove all other <style>
    html = str_replace_all(html, '<style[\\w\\W]*?>[\\w\\W]*</style>', '')
    writeLines(html, dest[i], useBytes = T)
}
message('There are some leftover to do:')
message('manually handle http://www.w3.org/StyleSheets/TR/2016/W3C-**, you can open a file to understand what I mean(W3C-**, base.css, **.svg)')
message('(optional) manually update http://www.w3.org/scripts/TR/2016/fixup.js')
message('(optional) manually update http://www.w3.org/TR/html51/styles/styles-html.css')
message('(optional) manually update http://www.w3.org/StyleSheets/TR/2016/logos/W3C')
}
#' @examples


update()
